//
//  MBSpaceCollectionMusicCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/21.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceDowningAllMusicCell.h"

@interface MBSpaceDowningAllMusicCell () {
    UIView                      *_bgView;
    UIButton                    *_selectBt;
    
    UIView                      *_contentView;
    UILabel                     *_nickNameLb;
    UIProgressView              *_progressView;
    UILabel                     *_musicSizeLb;
    UILabel                     *_musicTypeLb;
    UIView                      *_lineView;
    
    UIButton                    *_moreBt;
    
    NSString                    *_sizeName;
    float                       _progress;
}

@property (nonatomic,assign)BOOL isCompliling;   //是否编译
@property (nonatomic,assign)BOOL isALlSelecting; //是否全选

@end
@implementation MBSpaceDowningAllMusicCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //勾选按钮
    _selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBt.backgroundColor = [UIColor clearColor];
    _selectBt.hidden = YES;
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
    [_selectBt addTarget:self action:@selector(onGouXuanToSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_selectBt];
    
    //大模块 包含音乐标题、大小、类型以及下载图标
    _contentView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_contentView];
    
    //标题
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [_contentView addSubview:_nickNameLb];
    
    //进度条
    _progressView = [[UIProgressView alloc]initWithFrame:CGRectZero];
    _progressView.progressViewStyle = UIProgressViewStyleBar;
    _progressView.progressTintColor = TINK_COLOR;
    _progressView.trackTintColor = TRACK_COLOR;
    [_contentView addSubview:_progressView];
    
    //大小
    _musicSizeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _musicSizeLb.backgroundColor = [UIColor clearColor];
    _musicSizeLb.textAlignment = NSTextAlignmentLeft;
    _musicSizeLb.textColor = UIColorFromHex(0x999999);
    _musicSizeLb.font = [UIFont systemFontOfSize:11.f];
    [_contentView addSubview:_musicSizeLb];
    
    //类型
    _musicTypeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _musicTypeLb.backgroundColor = [UIColor clearColor];
    _musicTypeLb.textAlignment = NSTextAlignmentLeft;
    _musicTypeLb.textColor = UIColorFromHex(0x9999999);
    _musicTypeLb.font = [UIFont systemFontOfSize:11.f];
    [_contentView addSubview:_musicTypeLb];
    
    //线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.35];
    [_bgView addSubview:_lineView];
    
    //更多按钮
    _moreBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _moreBt.backgroundColor = [UIColor clearColor];
    [_moreBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [_moreBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
    [_moreBt addTarget:self action:@selector(onMoreToSelectAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_moreBt];
}

//更多选择
- (void)onMoreToSelectAction
{
    if (_moreBt.isHidden == NO) {
        if (_musicDownModel && _delegate && [_delegate respondsToSelector:@selector(onSelectItemForMoewWithValue:withView:)]) {
            [_delegate onSelectItemForMoewWithValue:_musicDownModel withView:self];
        }
    }
}

//单个勾选
- (void)onGouXuanToSelectAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForGouXuanWithValue:WithSelect:WithView:)]) {
        bt.selected = !bt.selected;
        [_delegate onSelectItemForGouXuanWithValue:_musicDownModel WithSelect:bt.selected WithView:self];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)onCompileCellForLayout:(BOOL)isCompile WithAllSelct:(BOOL)isAllSelct
{
    self.isCompliling = isCompile;
    self.isALlSelecting = isAllSelct;
    [self setNeedsLayout];
}

- (void)setIsCompliling:(BOOL)isCompliling
{
    _isCompliling = isCompliling;
    [self setNeedsLayout];
}

- (void)setIsALlSelecting:(BOOL)isALlSelecting
{
    _isALlSelecting = isALlSelecting;
    [self setNeedsLayout];
}

- (void)setIsHideMoewBt:(BOOL)isHideMoewBt
{
    _isHideMoewBt = isHideMoewBt;
    [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setIsSelect:(BOOL)isSelect
{
    _isSelect = isSelect;
    [self setNeedsLayout];
}

- (void)setMusicDownModel:(DownMusicModel *)musicDownModel
{
    _musicDownModel = musicDownModel;
    [self setNeedsLayout];
}

- (void)layoutDataWithSize:(NSString *)size withProgress:(float)progress
{
    _sizeName = size;
    _progress = progress;
    [self performSelectorOnMainThread:@selector(onUpdateUI) withObject:nil waitUntilDone:YES];
}

- (void)onUpdateUI
{
    if (_isAllStop == YES) {
        _progressView.progress = 0;
        _musicSizeLb.text = @"0 KB/s";
    } else {        
        _progressView.progress = _progress;
        _musicSizeLb.text = _sizeName;
    }
    float musicWidth = [_musicSizeLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_musicSizeLb.font} context:NULL].size.width+6;
    _musicSizeLb.frame = CGRectMake(_nickNameLb.left, _progressView.bottom+(WIDTH_5S ? 2 : 5), musicWidth, 20);
    _musicTypeLb.frame = CGRectMake(_musicSizeLb.right+10*PERCEN, _musicSizeLb.top, 140, 20);
}

- (void)setIsAllStop:(BOOL)isAllStop
{
    _isAllStop = isAllStop;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //value
    UIImage *selectNoImg = [UIImage imageNamed:@"weigouxuan_fb"];
    UIImage *selectImg = [UIImage imageNamed:@"xuanzhong_zl"];
    [_selectBt setImage:selectNoImg forState:UIControlStateNormal];
    [_selectBt setImage:selectImg forState:UIControlStateSelected];
    _selectBt.selected = _isALlSelecting ;//? _isALlSelecting:_isSelect;
    
    _nickNameLb.text = _musicDownModel.fileName;
    NSUInteger receivedSize = DownloadLength(_musicDownModel.url);
    //每秒下载速度
    if ([_musicDownModel.isDowning intValue] == 1) {
        NSTimeInterval downloadTime = -1 * [_musicDownModel.startTime timeIntervalSinceNow];
        NSUInteger speed = receivedSize / downloadTime;
        float speedSec = [_musicDownModel calculateFileSizeInUnit:(unsigned long long) speed];
        NSString *speedString = [_musicDownModel calculateUnit:(unsigned long long) speed];
        NSString *speedStr = [NSString stringWithFormat:@"%.2f%@/s",speedSec,speedString];
        CGFloat progress = 1.0 * receivedSize / _musicDownModel.totalLength;
        if (receivedSize == 0) {
            progress = 0.;
            speedStr = @"0 KB/s";
        }
        _musicSizeLb.text = speedStr;
        _progressView.progress = progress;
    } else {
        _musicSizeLb.text = @"0 KB/s";
        NSString *filePath = [MusicCachesDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",_musicDownModel.fileName,[_musicDownModel.url componentsSeparatedByString:@"."].lastObject]];
        NSUInteger receivedSize = DownloadLength(filePath);
        NSUInteger totalSize = _musicDownModel.totalLength;
        CGFloat progress = 1.0 * receivedSize / totalSize;
        _progressView.progress = progress;
    }

    
    _musicTypeLb.text = [_musicDownModel.musicType showMusicType];
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);
    _selectBt.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (_bgView.height-selectNoImg.size.height)/2., selectNoImg.size.width, selectNoImg.size.height);
    _selectBt.hidden = !_isCompliling;
    
    _contentView.frame = CGRectMake((_selectBt.isHidden ? _selectBt.left : _selectBt.right+10), 0, _bgView.width-125*PERCEN, _bgView.height);
    _nickNameLb.frame = CGRectMake(0, (WIDTH_5S ? 3 : 20*PERCEN), _contentView.width, 20);
    
    _moreBt.frame = CGRectMake(_bgView.width-100*PERCEN-(ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), (_bgView.height-20*PERCEN)/2., 100*PERCEN, 20*PERCEN);
    _moreBt.hidden = (_isHideMoewBt == NO ? !_selectBt.isHidden : YES);
    
    _lineView.frame = CGRectMake((ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), _bgView.height-1, (ISIPAD?_bgView.width-880*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):_bgView.width), 1);
    _progressView.frame = CGRectMake(_nickNameLb.left, _nickNameLb.bottom+(WIDTH_5S ? 4 : 5), (ISIPAD?1018*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):580*PERCEN), 2);
    
    float musicWidth = [_musicSizeLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_musicSizeLb.font} context:NULL].size.width+6;
    _musicSizeLb.frame = CGRectMake(_nickNameLb.left, _progressView.bottom+(WIDTH_5S ? 2 : 5), musicWidth, 20);
    _musicTypeLb.frame = CGRectMake(_musicSizeLb.right+10*PERCEN, _musicSizeLb.top, 140, 20);

}

@end
